<?php

namespace App\Admin\Controllers;

use App\Models\Class_section_group;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Auth\Database\Administrator;

class Class_groupController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.group'));
            $content->description(trans('admin.list'));
            
            $content->body($this->grid()->render());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.group'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.group'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Class_section_group::class, function (Grid $grid) {
            $grid->model()->where('type', '=', '2');
            $grid->id(trans('admin.ID'))->sortable();
            $grid->name(trans('admin.name'))->sortable();
            $grid->pid(trans('admin.pid'))->display(function($pid){
                if ($pid==0) {
                    return 'N/A';
                }else{
                    return Class_section_group::find($pid)->name;
                }
            })->sortable();
            $grid->description(trans('admin.description'))->display(function($description) {
                return str_limit($description, 30, '...');
            });

            $grid->actions(function ($actions) {
                $actions->disableView();
            });

            //禁用导出按钮
            $grid->disableExport();
            
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });
            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->like('name',trans('admin.name'));
                });
                $filter->column(1/2, function ($filter) {
                    $filter->equal('pid', trans('admin.pid'))->select(Class_section_group::where('pid','=','0')->get()->pluck('name', 'id'));
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Class_section_group::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->text('name', trans('admin.name'))->rules('required')->addElementLabelClass('must-input');
            $form->select('pid',trans('admin.pid'))->options(Class_section_group::where('pid','=','0')->get()->pluck('name', 'id'))->rules('required')->addElementLabelClass('must-input');
            $form->textarea('description', trans('admin.description'));
            $form->hidden('type', trans('admin.type'))->value('2');
            $form->multipleSelect('charge','分组负责人')->options(Administrator::all()->pluck('en_name','id'));
            //去掉添加/编辑表单的重置按钮
            $form->disableReset();
        });
    }
}
